home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 081 / upl.arc / PRIMES.UPL < prev    next >
Encoding:
Text File  |  1987-03-08  |  2.1 KB  |  59 lines

  1. ===============================================================================
  2. = Displays all prime numbers within a given range.  Not very useful in itself,=
  3. = but it's a good example of using simple math and control flow commands.     =
  4. ===============================================================================
  5.  
  6. ** Display syntax message if we weren't given any arguments **
  7.  
  8. ifeq @arg1 ""
  9.   print "Proper usage is: UPL @arg0 firstnumber lastnumber"
  10.   print
  11.   print "  where 'firstnumber' is the first number to be checked for being prime,"
  12.   print "  and 'lastnumber' is the last number to be checked."
  13.   abort
  14. endif
  15. end
  16. run
  17.  
  18. ** Initialize values for loop **
  19.  
  20. set @var0 @arg1             Initialize test value
  21. ifle @var0 2                2 is a special case;
  22.   print 2$09;                 force printing so
  23.   set @var0 3                 we can test only odd
  24.   goto GotOne                 numbers in the loop
  25. endif
  26. set @var9 @var0             If starting value
  27. mod @var9 2                 is an even number,
  28. ifeq @var9 0                it can't be prime, so
  29.   add @var0 1                 start with next odd
  30. endif
  31. set @var1 3                 Initialize to first potential factor
  32.  
  33. ** Test each number within the range by making sure it isn't evenly **
  34. ** divisible by any integer from 3 through its square root          **
  35.  
  36. Check4Factors:
  37. set @var9 @var0
  38. mod @var9 @var1
  39. ifeq @var9 0                If (VALUE MOD FACTOR)=0,
  40.   goto CheckNext              this is not a prime, reject it
  41. else
  42.   set @var9 @var0
  43.   div @var9 @var1
  44.   ifgt @var1 @var9          If FACTOR > (VALUE/FACTOR),
  45.     GotOne:                   we've tested all potential factors through
  46.     print @var0$09;           the square root without finding a valid one
  47.   else                      Else we haven't tested all possible factors,
  48.     add @var1 1               so try the next one
  49.     goto Check4Factors
  50.   endif
  51. endif
  52. CheckNext:
  53. add @var0 2                 Test the next odd number
  54. ifle @var0 @arg2            If next number is within range,
  55.   set @var1 3                 reset to first potential factor
  56.   goto Check4Factors          and perform test
  57. endif
  58. end
  59.